package com.qdlc.p2p.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.RedEnvelopeService;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.RedEnvelopeAttachedDao;
import com.qdlc.p2p.dal.dao.RedEnvelopeDao;
import com.qdlc.p2p.dal.dao.UserRedEnvelopeDao;
import com.qdlc.p2p.dal.dto.RedEnvelope;
import com.qdlc.p2p.dal.dto.RedEnvelopeAttached;
import com.qdlc.p2p.dal.model.RedEnvelopeModel;

/**
 * 红包方案 Service
 *
 * @author wuaj
 * @version 1.1.0.0
 */
@Service("redEnvelopeService")
public class RedEnvelopeServiceImpl implements RedEnvelopeService {

    @Resource
    private RedEnvelopeDao redEnvelopeDao;

    @Resource
    private RedEnvelopeAttachedDao redEnvelopeAttachedDao;

    @Resource
    private UserRedEnvelopeDao userRedEnvelopeDao;

    @Override
    public RedEnvelope save(RedEnvelopeModel model, List<RedEnvelopeAttached> list) {
        // 保存红包方案
        RedEnvelope redEnvelope = packageRedEnvelope(model);

        redEnvelope = redEnvelopeDao.save(redEnvelope);

        // 保存红包附属记录
        redEnvelopeAttachedDao.save(list, redEnvelope);

        return redEnvelope;
    }

    @Override
    public RedEnvelope getRedEnvelopeById(long id) {
        return redEnvelopeDao.getRedEnvelopeById(id);
    }

    @Override
    public List<RedEnvelope> findUsableRedEnvelope() {
        return redEnvelopeDao.findUsableRedEnvelope();
    }

    @Override
    public List<RedEnvelope> findUsableRedEnvelopeForFixed() {
        return redEnvelopeDao.findUsableRedEnvelopeForFixed();
    }

    @Override
    public PageDataList<RedEnvelopeModel> list(RedEnvelopeModel model) {
        return redEnvelopeDao.list(model);
    }

    @Override
    public RedEnvelope update(RedEnvelopeModel model, List<RedEnvelopeAttached> list) {
        //重新封装红包方案
        RedEnvelope redEnvelope = packageRedEnvelope(model);
        redEnvelope.setId(model.getId());
        redEnvelope.setAddTime(model.getAddTime());
        redEnvelope.setAddIp(model.getAddIp());
        redEnvelope.setLssueNum(model.getLssueNum());
        //修改红包方案
        redEnvelope = redEnvelopeDao.update(redEnvelope);

        //先移除红包附属信息
        redEnvelopeAttachedDao.deleteByRedEnvelopeId(redEnvelope.getId());

        //重新保存红包附属信息
        redEnvelopeAttachedDao.save(list, redEnvelope);

        return redEnvelope;
    }

    @Override
    public void delete(long id) {
        //删除红包方案
        redEnvelopeDao.delete(id);
        //删除红包附属信息
        redEnvelopeAttachedDao.deleteByRedEnvelopeId(id);
    }

    /**
     * 封装红包
     * 重新封装红包实体，防止属性误取值保存错误
     *
     * @param model RedEnvelopeModel
     * @return RedEnvelope
     */
    @SuppressWarnings("static-access")
    public RedEnvelope packageRedEnvelope(RedEnvelopeModel model) {
        RedEnvelope redEnvelope = new RedEnvelope();

        //固定金额
        if (model.getPayType() == model.RED_ENVELOPE_PAY_FIXED_AMOUNT)
            redEnvelope = new RedEnvelope(model.getName(), model.getStatus(), model.getType(), model.getPayType(),
                    model.getAmount(), model.getValidDay(), model.getTotal(), model.getDescription());

        //固定比例
        if (model.getPayType() == model.RED_ENVELOPE_PAY_FIXED_RATE)
            redEnvelope = new RedEnvelope(model.getName(), model.getStatus(), model.getType(), model.getPayType(),
                    model.getRate(), model.getMaxAmount(), model.getMinAmount(), model.getValidDay(), model.getTotal(), model.getDescription());

        //区间金额
        if (model.getPayType() == model.RED_ENVELOPE_PAY_INTERVAL_AMOUNT)
            redEnvelope = new RedEnvelope(model.getName(), model.getStatus(), model.getType(), model.getPayType(),
                    model.getValidDay(), model.getTotal(), model.getDescription());

        //区间比例
        if (model.getPayType() == model.RED_ENVELOPE_PAY_INTERVAL_RATE)
            redEnvelope = new RedEnvelope(model.getName(), model.getStatus(), model.getType(), model.getPayType(),
                    model.getMaxAmount(), model.getMinAmount(), model.getValidDay(), model.getTotal(), model.getDescription());

        return redEnvelope;
    }

    @Override
    public int changeStatus(long[] redEnvelopeIds, int preStatus, int aftStatus) {
        return userRedEnvelopeDao.changeStatus(redEnvelopeIds, preStatus, aftStatus);
    }
}
